﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;

namespace CommunitySystem
{
    public partial class ChengJiWindow_ZL : ChildWindow
    {
        private ZhaoLuService.ZhaoLuServiceClient Client1 = new ZhaoLuServiceClient();
        private ZhaoLu_DaoRuKaoShengModel model = new ZhaoLu_DaoRuKaoShengModel();
        public int ID;
        public ChengJiWindow_ZL(int id)
        {
            InitializeComponent();
            ID = id;
            TextBlock t = new TextBlock();
            mianshi.IsEnabled = true;
            Client1.GetZhaoLu_DaoRuKaoShengModelCompleted += new EventHandler<GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs>(Client1_GetZhaoLu_DaoRuKaoShengModelCompleted);
            Client1.UpdateZL_MianShiFenShuCompleted += new EventHandler<UpdateZL_MianShiFenShuCompletedEventArgs>(Client1_UpdateZL_MianShiFenShuCompleted);
            Load();
        }
        public void Load()
        {
            Client1.GetZhaoLu_DaoRuKaoShengModelAsync(0, false, 0, ID);
        }
        void Client1_GetZhaoLu_DaoRuKaoShengModelCompleted(object sender, GetZhaoLu_DaoRuKaoShengModelCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
                this.DataContext = model = e.Result[0];
        }
        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                double.Parse(((TextBox)sender).Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                ((TextBox)sender).Text = "";
            }
        }

        private void Text_LostFocus(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox)sender).Text))
                if (!string.IsNullOrEmpty(bishi.Text))
                {
                    total.Text =
                        Math.Round(
                            (Math.Round(Convert.ToDouble(bishi.Text) * 0.4, 2) +
                             Math.Round(Convert.ToDouble(mianshi.Text) * 0.6, 2)), 2).ToString();
                    model.TotalScore = Convert.ToDecimal(total.Text);
                }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToDecimal(bishi.Text) > 100 || Convert.ToDecimal(mianshi.Text) > 100)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("输入的成绩有误，请重新输入！");
                return;
            }
            Client1.UpdateZL_MianShiFenShuAsync(model);
        }

        void Client1_UpdateZL_MianShiFenShuCompleted(object sender, UpdateZL_MianShiFenShuCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功!");
            this.DialogResult = true;
        }
    }
}
